<!DOCTYPE html>
<html lang="en">
<head>
    {template 'common/edithead'}
    <!--微擎 -->
    <!--这个放在最后一切平白安全-->
    <script type="text/javascript" src="./resource/js/require.js?v=20170915"></script>
    <script type="text/javascript">
        //编辑器里使用
        var urobj=[];
    </script>
</head>
<body class="nav-md" style="min-width: 800px">
<div>
    <div class="container xc_edit_from" >
        <div class="main_container" style="overflow-x: hidden;min-height: calc(100vh - 20px);background-color: white;width: 100%;">
            <div class="col-md-12 col-xs-12">
                <div class="x_panel">
                    <ul class="we7-page-tab">
                        <li >
                            <a  href="{php echo $this->createWebUrl($do, array('op'=>'list','xtitleb'=>urlencode($xtitleb),'xtitlea'=>urlencode($xtitlea),'new'=>1));}">
                                {$xtitleb}- {$xtitlea}列表  </a></li>
                        <li><a a href="{php echo $this->createWebUrl($do, array('op'=>'edit','xtitleb'=>urlencode($xtitleb),'xtitlea'=>urlencode($xtitlea),'new'=>1));}">{$xtitleb}- {$xtitlea}增加</a></li>
                    </ul>
                    <div class="x_content">
                        <br/>
                        <form id="sign-form" class="form-horizontal" role="form" method="post" name="submit">
                            <input type="hidden" name="id" value="{$xc['id']}">
                            <input type="hidden" name="xc[video]" value="">
                            <input type="hidden" name="xc[audio]" value="">
                            <div class="form-group">
                                <label for="name" class="col-sm-2 control-label">名称</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control"  name="xc[name]" id="name" value="{$xc['name']}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">分类</label>
                                <div class="col-sm-8">
                                    <select  class="form-control js-select2" name="xc[cid]" data-select="{$xc['cid']}">
                                        <option value="">请选择分类</option>
                                        {loop $class $index $item}
                                        <option value="{$item['id']}" {if $item['id']==$xc['cid']}selected{/if} >{$item['name']}</option>
                                        {/loop}
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">封面</label>
                                <div class="col-sm-8">
                                    {php echo tpl_form_field_image('xc[simg]',$xc['simg']);}
                                    <span class="help-block">750*360</span>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">价格</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control"  name="xc[price]" value="{$xc['price']}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">活动时间</label>
                                <div class="col-sm-8">
                                    {php echo tpl_form_field_daterange('times',$xc['times'],true);}
                                </div>
                            </div>
                            <div class="form-group param" data-name="xc[video]">
                                <label  class="col-sm-2 control-label">视频课程</label>
                                <div class="col-sm-8">
                                    {if $xc['video']}
                                    {loop $xc['video'] $index $item}
                                    <div class="input-group" style="margin-bottom: 10px;">
                                        <input type="hidden" class="form-control" data-name="id" value="{$item['id']}">
                                        <input type="text" class="form-control" data-name="name" value="{$item['name']}">
                                        <span class="input-group-btn">
                                            <button class="btn btn-default link" type="button" data-toggle="modal" data-target="#sort_link">选择课程</button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_add(this)">
                                            <button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_del(this)">
                                            <button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>
                                        </span>
                                    </div>
                                    {/loop}
                                    {else}
                                    <div class="input-group" style="margin-bottom: 10px;">
                                        <input type="hidden" class="form-control" data-name="id" value="">
                                        <input type="text" class="form-control" data-name="name" value="">
                                        <span class="input-group-btn">
                                            <button class="btn btn-default link" type="button" data-toggle="modal" data-target="#sort_link">选择课程</button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_add(this)">
                                            <button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_del(this)">
                                            <button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>
                                        </span>
                                    </div>
                                    {/if}
                                </div>
                            </div>
                            <div class="form-group param" data-name="xc[audio]">
                                <label  class="col-sm-2 control-label">音频课程</label>
                                <div class="col-sm-8">
                                    {if $xc['audio']}
                                    {loop $xc['audio'] $index $item}
                                    <div class="input-group" style="margin-bottom: 10px;">
                                        <input type="hidden" class="form-control" data-name="id" value="{$item['id']}">
                                        <input type="text" class="form-control" data-name="name" value="{$item['name']}">
                                        <span class="input-group-btn">
                                            <button class="btn btn-default link" type="button" data-toggle="modal" data-target="#sort_audio">选择课程</button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.audio_add(this)">
                                            <button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_del(this)">
                                            <button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>
                                        </span>
                                    </div>
                                    {/loop}
                                    {else}
                                    <div class="input-group" style="margin-bottom: 10px;">
                                        <input type="hidden" class="form-control" data-name="id" value="">
                                        <input type="text" class="form-control" data-name="name" value="">
                                        <span class="input-group-btn">
                                            <button class="btn btn-default link" type="button" data-toggle="modal" data-target="#sort_audio">选择课程</button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.audio_add(this)">
                                            <button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>
                                        </span>
                                        <span class="input-group-btn" onclick="parameter.parameter_del(this)">
                                            <button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>
                                        </span>
                                    </div>
                                    {/if}
                                </div>
                            </div>
                            <script>
                                var parameter={
                                    parameter_add:function(objc){
                                        $(objc).parent().after('<div class="input-group" style="margin-bottom: 10px;">' +
                                                '<input type="hidden" class="form-control" data-name="id" value="">' +
                                                '<input type="text" class="form-control" data-name="name" value="">' +
                                                '<span class="input-group-btn">' +
                                                '<button class="btn btn-default link" type="button" data-toggle="modal" data-target="#sort_link">选择课程</button>' +
                                                '</span>' +
                                                '<span class="input-group-btn" onclick="parameter.parameter_add(this)">' +
                                                '<button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>' +
                                                '</span>' +
                                                '<span class="input-group-btn" onclick="parameter.parameter_del(this)">' +
                                                '<button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>' +
                                                '</span>' +
                                                '</div>')
                                    },
                                    audio_add:function(objc){
                                        $(objc).parent().after('<div class="input-group" style="margin-bottom: 10px;">' +
                                                '<input type="hidden" class="form-control" data-name="id" value="">' +
                                                '<input type="text" class="form-control" data-name="name" value="">' +
                                                '<span class="input-group-btn">' +
                                                '<button class="btn btn-default link" type="button" data-toggle="modal" data-target="#sort_audio">选择课程</button>' +
                                                '</span>' +
                                                '<span class="input-group-btn" onclick="parameter.audio_add(this)">' +
                                                '<button class="btn btn-default" type="button"><i class="fa fa-plus"></i></button>' +
                                                '</span>' +
                                                '<span class="input-group-btn" onclick="parameter.parameter_del(this)">' +
                                                '<button class="btn btn-danger" type="button"><i class="fa fa-remove"></i></button>' +
                                                '</span>' +
                                                '</div>')
                                    },
                                    parameter_del:function(objc){
                                        if($(objc).parent().siblings().length>0){
                                            $(objc).parent().remove();
                                        }
                                    }
                                }
                            </script>
                            {if $share['status']==1}
                            {if $share['type']>=1}
                            <div class="form-group">
                                <label class="col-sm-2 control-label">一级分销佣金</label>
                                <div class="col-sm-8">
                                    <div class="input-group">
                                        <span class="input-group-btn">
                                            <button class="btn btn-default" type="button">消费金额的</button>
                                        </span>
                                        <input type="text" class="form-control" name="xc[share_one]" value="{$xc['share_one']}" placeholder="佣金">
                                        <span class="input-group-btn">
                                            <button class="btn btn-default" type="button">%</button>
                                        </span>
                                    </div>
                                </div>
                            </div>
                            {/if}
                            {if $share['type']>=2}
                            <div class="form-group">
                                <label class="col-sm-2 control-label">二级分销佣金</label>
                                <div class="col-sm-8">
                                    <div class="input-group">
                                        <span class="input-group-btn">
                                            <button class="btn btn-default" type="button">消费金额的</button>
                                        </span>
                                        <input type="text" class="form-control" name="xc[share_two]" value="{$xc['share_two']}" placeholder="佣金">
                                        <span class="input-group-btn">
                                            <button class="btn btn-default" type="button">%</button>
                                        </span>
                                    </div>
                                </div>
                            </div>
                            {/if}
                            {if $share['type']>=3}
                            <div class="form-group">
                                <label class="col-sm-2 control-label">三级分销佣金</label>
                                <div class="col-sm-8">
                                    <div class="input-group">
                                        <span class="input-group-btn">
                                            <button class="btn btn-default" type="button">消费金额的</button>
                                        </span>
                                        <input type="text" class="form-control" name="xc[share_three]" value="{$xc['share_three']}" placeholder="佣金">
                                        <span class="input-group-btn">
                                            <button class="btn btn-default" type="button">%</button>
                                        </span>
                                    </div>
                                </div>
                            </div>
                            {/if}
                            {/if}
                            <div class="form-group">
                                <label class="col-sm-2 control-label">状态</label>
                                <div class="col-sm-8">
                                    <input type="checkbox" class="js-switch" value="1" name="xc[status]"
                                           data-value="{$xc['status']}" data-field="status">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">排序</label>
                                <div class="col-sm-8">
                                    <input type="text" class="form-control"  name="xc[sort]" value="{$xc['sort']}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">详情</label>
                                <div class="col-sm-8">
                                    {php echo tpl_ueditor('xc[content]',$xc['content']);}
                                </div>
                            </div>
                            <div class="form-group">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <input type="button" name="submit" class="btn btn-default" value="提交">
                                    <a type="button" class="btn btn-default"  href="{php echo $this->createWebUrl($do, array('op'=>'list','xtitleb'=> urlencode($xtitleb),'xtitlea'=>urlencode($xtitlea),'new'=>1));}" >返回</a>
                                    <input id="res" name="res" type="reset" style="display:none;" />
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="sort_link"><div class="modal-dialog">
    <style>
        #sort_link .modal-body {padding: 10px 15px;}
        #sort_link .tab-pane {margin-top: 5px; min-height: 400px; max-height: 400px; overflow-y: auto;}
        #sort_tab{margin-bottom: 10px;}
    </style>
    <div class="modal-content">
        <div class="modal-header">
            <button data-dismiss="modal" class="close" type="button">×</button>
            <h4 class="modal-title">视频课程</h4>
        </div>
        <div class="modal-body">
            <iframe width="100%" height="395" frameborder="no" border="0" scrolling="no" allowtransparency="yes" src="{url 'site/entry/'.$_GPC['do'],array('m'=>$_GPC['m'],'op'=>'video')}"></iframe>
        </div>
        <div class="modal-footer">
            <button data-dismiss="modal" class="btn btn-default" id="sort_close" type="button">关闭</button>
        </div>
    </div>
</div></div>
<div class="modal fade" id="sort_audio"><div class="modal-dialog">
    <style>
        #sort_audio .modal-body {padding: 10px 15px;}
        #sort_audio .tab-pane {margin-top: 5px; min-height: 400px; max-height: 400px; overflow-y: auto;}
        #sort_tab{margin-bottom: 10px;}
    </style>
    <div class="modal-content">
        <div class="modal-header">
            <button data-dismiss="modal" class="close" type="button">×</button>
            <h4 class="modal-title">音频课程</h4>
        </div>
        <div class="modal-body">
            <iframe width="100%" height="395" frameborder="no" border="0" scrolling="no" allowtransparency="yes" src="{url 'site/entry/'.$_GPC['do'],array('m'=>$_GPC['m'],'op'=>'audio')}"></iframe>
        </div>
        <div class="modal-footer">
            <button data-dismiss="modal" class="btn btn-default" id="audio_close" type="button">关闭</button>
        </div>
    </div>
</div></div>
{template 'common/editfoot'}
<script>
    var objc="";
    $(function(){
        $("input[name='submit']").click(function(){
            getJson();
            var data=$(".form-horizontal").serialize();
            $.ajax({
                type:"post",
                url:"{url 'site/entry/'.$_GPC['do'],array('m'=>$_GPC['m'],'op'=>'savemodel','version_id'=>$_GPC['version_id'])}",
                data:data,
                dataType:'json',
                success:function(res){
                    if(res.status==1){
                        if($("input[name='id']").val()==""){
                            $("input[name='res']").click();
                            $("body").find(".img-responsive.img-thumbnail").attr("src","");
                        }
                        swal('操作成功!', '操作成功!', 'success');
                    }else{
                        swal('操作失败!', '操作失败!', 'error');
                    }
                }
            })
        });
        $("body").on("click",".link",function(){
            objc=this;
        });
    });
    function getJson(){
        $(".param").each(function(){
            var name=$(this).attr("data-name");
            var data=[];
            $(this).find(".input-group").each(function(){
                var data_item={};
                $(this).find("input").each(function(){
                    if($(this).val()!=""){
                        data_item[$(this).attr("data-name")]=$(this).val();
                    }
                });
                if(JSON.stringify(data_item)!="{}"){
                    data.push(data_item);
                }
            });
            if(data.length>0){
                $("input[name='"+name+"']").val(JSON.stringify(data));
            }
        });
    }
</script>
</body>
</html>